<?php

namespace Home\Model\Ajax;

class EditUsername extends AbstractAjax
{
    // 修改用户名
    public function handle()
    {
        if (empty($this->uid)) {
            $ajax['status'] = 0;
            $ajax['content'] = "获取UID失败";
            return $ajax;
        } else {
            $edit = I('request.edit', '0', 'intval');
            if ($edit) {
                $username = I('request.username', '', 'trim');
                if (strlen($username) < 6 || strlen($username) > 18) {
                    $ajax['status'] = 0;
                    $ajax['content'] = '用户名长度为6-18位字符之间';
                    return $ajax;
                }
                $info = D('User')->edit_username($this->uid, $username);
                if ($info['status'] == 1) {
                    session('ytata.username', $username);
                    $ajax['status'] = 1;
                    $ajax['username'] = $username;
                    $ajax['content'] = '修改成功！';
                    return $ajax;

                } else {
                    $ajax['status'] = 0;
                    $ajax['content'] = $info['content'];
                    return $ajax;
                }
            } else {
                $this->assign('username', session('ytata.username'));
                $html = $this->fetch('ajax_edit_username');
                $ajax['status'] = 1;
                $ajax['content'] = $html;
                return $ajax;
            }
        }
    }

}